package com.commerce.controller;

import com.commerce.dao.CommodityDao;
import com.commerce.enums.ErrorCode;
import com.commerce.exception.ResultException;
import com.commerce.pojo.dto.CommodityDTO;
import com.commerce.pojo.dto.Result;
import com.commerce.pojo.po.Commodity;
import com.commerce.pojo.po.User;
import com.commerce.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @GetMapping("/index")
    public Result index(@RequestParam(name = "CommodityName", defaultValue = "") String CommodityName) {
        List list = commodityService.selectList(CommodityName);
        return new Result(200, "查询商品", list);
    }

    @GetMapping("/commodity/{id}")
    public Result getById(@PathVariable Integer id,@RequestParam(name = "UserId") Integer UserId) {
        CommodityDTO commodity = new CommodityDTO();
        commodity = commodityService.getOneById(id,UserId);
        return new Result(200, "单个商品详情",commodity);
    }
    @GetMapping("/commodity/myShop")
    public Result getCommodityByUser(@RequestParam(name = "UserId")Integer UserId){
        List<Commodity> commodityList = commodityService.getByUser(UserId);
        return new Result(200,"该用户的全部商品",commodityList);
    }

    @PostMapping("/commodity/save")
    public Result upload(@RequestParam("uploadfile")MultipartFile uploadfile,
                         @RequestParam("name")String name,
                         @RequestParam("price")double price ,
                         @RequestParam("state")Integer state,
                         @RequestParam("intro")String intro,
                         @RequestParam("detail")String detail,
                         @RequestParam("userId")Integer userId,
                         @RequestParam("userName")String userName
                         ) throws IOException {
        InputStream inputStream = uploadfile.getInputStream();
        FileChannel inChannel = (FileChannel) Channels.newChannel(inputStream);
        if(name==null)
            throw new ResultException(ErrorCode.UPLOAD_FAILURE);
        String fName = name+".jpg";
        FileChannel outChannel = new FileOutputStream("./imgs/"+fName).getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
        //3、关闭流
        inChannel.close();
        outChannel.close();
        inputStream.close();
        String img = "http://47.115.227.64:8080/images/"+fName;
        Commodity commodity = new Commodity(img,intro,detail,name,price,state,userId,userName);
        commodityService.save(commodity);
        return new Result().ok();
    }

    @DeleteMapping("/commodity/{id}")
    public Result delete(@PathVariable Integer id){
        commodityService.removeById(id);
        return new Result().ok();
    }


}
